//
//  InputText4CommentViewController.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/30.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "InputText4CommentViewController.h"

@interface InputText4CommentViewController ()

/// 模型视图
@property (nonatomic, readonly, strong) InputText4CommentViewModel* viewModel;

// UI
@property (nonatomic, weak) IBOutlet UIButton* bgButton;
@property (nonatomic, weak) IBOutlet UIButton* publishButton;
@property (nonatomic, weak) IBOutlet UIView* textPView;

@end

@implementation InputText4CommentViewController
@dynamic viewModel;

- (instancetype)initWithViewModel:(SUViewModel2 *)viewModel
{
    self = [super initWithViewModel:viewModel];
    if (self)
    {
        [self initData];
        [self initLoadForRAC];
        [self initUI];
    }
    return self;
}

- (void)initData
{
    
}

- (void)initLoadForRAC
{
    @weakify(self)
    [[self rac_signalForSelector:@selector(viewDidLoad)] subscribeNext:^(id x)
     {
         @strongify(self)
         
     }];
}

- (void)initUI
{
    
}

- (void)bindViewModel
{
    [super bindViewModel];
    
    @weakify(self)
    
    RACSignal* willAppearSignal = [[self rac_signalForSelector:@selector(viewWillAppear:)] replay];
    RACSignal* didAppearSignal = [[self rac_signalForSelector:@selector(viewDidAppear:)] replay];
    RACSignal* viewDidLayoutSVSignal = [[self rac_signalForSelector:@selector(viewDidLayoutSubviews)] replay];
    
    [willAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
        self.view.backgroundColor = [UIColor clearColor];
    }];
    
    [didAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
        NSLog(@"viewDidAppear");
        
        // ScrollView 内容 未能在此时才约束
        
        //Request
        
    }];
    
    [[self rac_signalForSelector:@selector(viewWillDisappear:)] subscribeNext:^(id x)
     {
         @strongify(self)
         
     }];
    
    [[[[viewDidLayoutSVSignal take:1] zipWith:didAppearSignal] deliverOnMainThread] subscribeNext:^(id x)
     {
         @strongify(self)
         
         //重置 更新 frame
         [self _setupAllView];
     }];
    
#pragma mark -- bindViewModel
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
}

- (void)_setupAllView
{
    @weakify(self);
    
    YYTextView* tView = [[YYTextView alloc] initWithFrame:(CGRect) {0, 0, self.textPView.frame.size}];
    tView.font = [UIFont systemFontOfSize:16.0f];
    tView.placeholderFont = [UIFont systemFontOfSize:16.0f];
    tView.placeholderText = @"请输入...";
    tView.placeholderTextColor = UIColorHex(#e8e8e8);
    tView.tag = 1000;
    [self.textPView addSubview:tView];
    RAC(self.viewModel, strText) = RACObserve(tView, text);
    self.textPView.layer.cornerRadius = 5.0f;
    self.textPView.layer.borderColor = SUGlobalGrayBackgroundColor.CGColor;
    self.textPView.layer.borderWidth = 1.0f;
    self.textPView.layer.masksToBounds = YES;
    
    [[self.bgButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x)
    {
        @strongify(self);
        
        [self _btnListener_bg];
    }];
    [[self.publishButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x)
    {
        @strongify(self);
        
        [self _btnListener_publish];
    }];
}

#pragma mark - Listener
- (void)_btnListener_bg
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)_btnListener_publish
{
    [self.view resignFirstResponder];
    
    [self.viewModel.publishCmd execute:nil];
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark - Delegate
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
